package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.item.pojo.Brand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.common.parameter.BrandQueryByPageParameter;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    private BrandMapper brandMapper;

    //分页查询
//    @Override
//    public PageResult<Brand> queryBrandByPageAndSort(
//            Integer page, Integer rows, String sortBy, Boolean desc, String key) {
//        // 开始分页
//        PageHelper.startPage(page, rows);
//        // 过滤
//        Example example = new Example(Brand.class);
//        if (StringUtils.isNotBlank(key)) {
//            example.createCriteria().orLike("name", "%" + key + "%");
//            example.createCriteria().orEqualTo("letter", key);
//        }
//        // 排序
//        if (StringUtils.isNotBlank(sortBy)) {
//            String sortByClause = sortBy + (desc ? " DESC" : " ASC");
//            example.setOrderByClause(sortByClause);
//        }
//        // 查询
//        List<Brand> brandList = brandMapper.selectByExample(example);
//        // 创建pageInfo封装brandList
//        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
//        // 返回结果
//        PageResult<Brand> result = new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
//        return result;
//    }

    //分页查询
    @Override
    public PageResult<Brand> queryBrandByPageAndSort(BrandQueryByPageParameter param) {
        //1. 分页
        PageHelper.startPage(param.getPage(),param.getRows());
        //2.排序 模糊查询
        Example example=new Example(Brand.class);
        Example.Criteria criteria=example.createCriteria();
        if(StringUtils.isNotBlank(param.getSortBy())){
            example.setOrderByClause(param.getSortBy()+(param.getDesc()?" DESC":" ASC"));
        }
        if(StringUtils.isNotBlank(param.getKey())){
            criteria.orLike("name","%"+param.getKey()+"%").orEqualTo("letter",param.getKey());
        }
        //3.查询数据
        List<Brand> brands = brandMapper.selectByExample(example);
        PageInfo<Brand> pageInfo=new PageInfo<>(brands);
        //4.返回分页结果
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }


    //新增
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBrand(Brand brand, List<Long> cids) {
        brandMapper.insertSelective(brand);
        for (Long cid : cids) {
            brandMapper.insertCategoryBrand(cid, brand.getId());
        }
    }

    //更新
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBrand(Brand brand, List<Long> categories) {
        //修改品牌表
        brandMapper.updateByPrimaryKeySelective(brand);
        //维护中间表
        for (Long cid : categories) {
            //brandMapper.deleteCategoryBrand(brand.getId());
            brandMapper.insertCategoryBrand(cid, brand.getId());
        }
    }

    /**
     * 品牌删除
     * @param bid
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBrand(Long bid) {
        brandMapper.deleteByPrimaryKey(bid);
        //维护中间表
        this.brandMapper.deleteByBrandIdInCategoryBrand(bid);
    }


    //删除中间表数据
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByBrandIdInCategoryBrand(Long bid) {
        brandMapper.deleteByBrandIdInCategoryBrand(bid);
    }

    //根据cid查询品牌
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Brand> queryBrandByCategoryId(Long cid) {
        return brandMapper.queryBrandByCategoryId(cid);
    }

    //根据品牌id集合查询品牌信息
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Brand> queryBrandByBrandIds(List<Long> ids) {
        List<Brand> brandList = new ArrayList<>();
        for (Long id : ids) {
            Brand brand = brandMapper.selectByPrimaryKey(id);
            brandList.add(brand);
        }
        return brandList;
    }
}
